package com.taotao.tools.core.bean;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * <p>
 * 实体类工具类
 * </p>
 *
 * @author youbeiwuhuan
 * @version V1.0
 * @since 2019/6/25 19:35
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class EntityUtils {
    private static final Logger log = LoggerFactory.getLogger(EntityUtils.class);


    private static final int YES = 1;
    private static final int NO = 0;

    private static final String CREATE_USER_SETTER = "setCreateId";
    private static final String CREATE_TIME_SETTER = "setCreateTime";
    private static final String DELETED_FLAG_SETTER = "setIsDel";
    private static final String UPDATE_USER_SETTER = "setUpdateId";
    private static final String UPDATE_TIME_SETTER = "setUpdateTime";

    /**
     * 设置5个字段默认值
     *
     * @param userId 用户ID
     * @param now    当前时间
     * @param target 对象
     */
    public static void setPo5Field(Long userId, Date now, Object target) {
        if (null == userId) {
            throw new IllegalArgumentException();
        }

        try {
            inmokeSetMedthod(CREATE_USER_SETTER, userId, target);
            inmokeSetMedthod(CREATE_TIME_SETTER, now, target);
            inmokeSetMedthod(DELETED_FLAG_SETTER, NO, target);
            inmokeSetMedthod(UPDATE_USER_SETTER, userId, target);
            inmokeSetMedthod(UPDATE_TIME_SETTER, now, target);
        } catch (Exception e) {
            log.error("",e);
        }

    }

    /**
     * 设置4个字段默认值
     *
     * @param userId 用户ID
     * @param now    当前时间
     * @param target 对象
     */
    public static void setPo4Field(Long userId, Date now, Object target) {
        if (null == userId) {
            throw new IllegalArgumentException();
        }

        try {
            inmokeSetMedthod(CREATE_USER_SETTER, userId, target);
            inmokeSetMedthod(CREATE_TIME_SETTER, now, target);
            inmokeSetMedthod(UPDATE_USER_SETTER, userId, target);
            inmokeSetMedthod(UPDATE_TIME_SETTER, now, target);
        } catch (Exception e) {
            log.error("",e);
        }
    }

    /**
     * 设置3个字段默认值
     *
     * @param userId 用户ID
     * @param now    当前时间
     * @param target 对象
     */
    public static void setPo3Field(Long userId, Date now, Object target) {

        if (null == userId) {
            throw new IllegalArgumentException();
        }

        try {
            inmokeSetMedthod(DELETED_FLAG_SETTER, NO, target);
            inmokeSetMedthod(UPDATE_USER_SETTER, userId, target);
            inmokeSetMedthod(UPDATE_TIME_SETTER, now, target);
        } catch (Exception e) {
            log.error("",e);
        }
    }

    /**
     * 设置2个字段默认值
     *
     * @param userId 用户ID
     * @param now    当前时间
     * @param target 对象
     */
    public static void setPo2Field(Long userId, Date now, Object target) {

        if (null == userId) {
            throw new IllegalArgumentException();
        }

        try {
            inmokeSetMedthod(UPDATE_USER_SETTER, userId, target);
            inmokeSetMedthod(UPDATE_TIME_SETTER, now, target);
        } catch (Exception e) {
            log.error("",e);
        }
    }

    /**
     * 执行方法
     *
     * @param methodName 方法名
     * @param arg        参数
     * @Param target 执行对象
     */
    private static Object inmokeSetMedthod(final String methodName, Object arg, Object target)
            throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Method method = target.getClass().getDeclaredMethod(methodName, arg.getClass());
        return method.invoke(target, arg);
    }
}
